package org.tcshare.app;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.orhanobut.logger.Logger;

import org.tcshare.logutils.HttpLogger;
import org.tcshare.logutils.MyAndroidLogAdapter;
import org.tcshare.network.HttpApi;
import org.tcshare.network.HttpLogInterceptor;
import org.tcshare.network.cookie.CookieJarImpl;
import org.tcshare.network.cookie.MemoryCookieStore;
import org.tcshare.network.cookie.PersistentCookieStore;
import org.tcshare.utils.ShellUtils;
import org.tcshare.utils.UnZipUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.CookieJar;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by FallRain on 2017/8/21.
 */

public class App extends Application {
    public static Context ctx;

    @Override
    public void onCreate() {
        super.onCreate();
        ctx = this;

        // 测试php
        new Thread(){
            @Override
            public void run() {
                super.run();
                File serverDir = new File(getFilesDir(), "server");
                String path = "/data/data/" + getPackageName() + "/files/server";
                String cmdBusybox = path + "/busybox ";
                String cmdPhp = path + "/php ";
                String cmdStart = path + "/start.sh ";
                String cmdStop = path + "/stop.sh ";

                if(!serverDir.isDirectory()){
                    try {
                        if (UnZipUtil.unZip(getAssets().open("server.zip"), getFilesDir().getPath())) {
                            ShellUtils.CommandResult chmodRet = ShellUtils.execCommand(new String[]{
                                    "chmod 755 -R " + path,
                                    "chmod 755 " + cmdStart,
                                    "chmod 755 " + cmdStop,
                                    "chmod 755 " + cmdBusybox,
                                    "chmod 755 " + cmdPhp}, true);
                            Log.e("TAG", "chmod: " + chmodRet.toString());
                        } else {
                            Log.e("TAG", "解压失败！");
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e("TAG", "打开压缩文件失败！" + e.getMessage());
                    }

                }

                ShellUtils.CommandResult startServerRet = ShellUtils.execCommand(new String[]{cmdStop + path, cmdStart + path}, true);
                Log.e("TAG", "startServer:" + startServerRet.toString());
            }
        }.start();
    }
}
